<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\Mysql;
$database = Mysql::init();

if (isset($_POST["nickname"]) && isset($_POST["password"]) && isset($_POST["captcha"])) {
    $username = $_POST["nickname"];
    $userpasswd = $_POST["password"];
    $code = $_POST["captcha"];
    if ($code != $_SESSION["Code"]) {
        echo "验证码错误";
    } else {
        $repname = $database->select("user_text", "id", ["username" => $username]);
        if ($repname[0] != null) {
            echo "<script>alert('用户名重复!');location.href='/admin/register.php';</script>";
        } else {
            $cgaracrers = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $salt = '';
            for ($i = 0; $i < 6; $i++) {
                $salt .= $cgaracrers[rand(0, strlen($cgaracrers) - 1)];
            }
            $encrypted_data1 = md5($userpasswd);
            $encrypted_data2 = hash("sha512", $encrypted_data1 . $salt);
            $encrypted_data = hash("sha512", $encrypted_data2);
            $database->insert("user_text", ["username" => $username, "password" => $encrypted_data, "static" => "否"]);
            $id = $database->select("user_text", "id", ["username" => $username]);
            $database->insert("pass_key", ["uid" => $id[0], "salt" => $salt]);
            echo "<script>alert('注册成功!,请联系管理员启用账户，并在个人资料设置处完善个人资料。');location.href='/login.html';</script>";
        }
    }
}
?>